﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FangChanCrm.Services.Interface;
using FangChanCrmModel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace FangChanCrm.Controllers
{
    public class PhoneBeiWangLuController : Controller
    {
        private readonly It_BeiWangLuService t_BeiWangLuService;

        public PhoneBeiWangLuController(It_BeiWangLuService beiWangLuService)
        {
            t_BeiWangLuService = beiWangLuService;
        }

        public IActionResult CreateAndModify()
        {
            return View();
        }
        public IActionResult List()
        {
            ViewData[""] = "";
            return View();
        }
        [HttpPost]
        public string Add()
        {
            var dt = Convert.ToDateTime(Request.Form["Time"]).ToString();
            FangChanCrmModel.t_BeiWangLu t_BeiWangLu = new FangChanCrmModel.t_BeiWangLu
            {
                BiaoTi = Request.Form["BiaoTi"],
                NeiRong = Request.Form["NeiRong"],
                RenYuanId = Convert.ToInt32(HttpContext.Session.GetString("loginid")),
                TiXingShiJian = dt
            };
            if (t_BeiWangLuService.Add(t_BeiWangLu).Id > 0)
            {
                return "success";
            }
            else
            {
                return "error";
            }
        }
        [HttpPost]
        public string GetList()
        {
            var list = t_BeiWangLuService.GetList(Convert.ToInt32(HttpContext.Session.GetString("loginid"))).OrderByDescending(x => x.Id).ToList();
            return JsonConvert.SerializeObject(list, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }
        [HttpPost]
        public string GetById()
        {
            var list = new List<t_BeiWangLu>();
            list.Add(t_BeiWangLuService.GetById(Convert.ToInt32(Request.Form["Id"])));
            return JsonConvert.SerializeObject(list, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }
    }
}
